<template>
  <!-- navigation设置 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row></template>
        <template v-slot:actions>
          <el-button v-permission="'game.add'" type="primary" size="mini" icon="el-icon-plus" @click="isShowDetails = true, checkedItem = null">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button>
        </template>
        <template>
          <el-table-column align="center" type="index" width="50">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isChecked" :label="true" @change="handleCurrentChange(scope.row)">{{""}}</el-radio>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="id" label="ID" width="90"/>
          <el-table-column align="center" prop="name" label="导航名称" width="180">
            <template slot-scope="scope">
              {{ languageArrayMap.get(scope.row.name) }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="types" label="类型" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.types==1?'success':'info'">{{ scope.row.types==1?'web左侧导航':'web中间导航' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="parent_id" label="上级ID" width="120"/>
          <el-table-column align="center" prop="icon" label="图片" show-overflow-tooltip>
            <template slot-scope="scope">
              <img v-if="scope.row.icon" :src="scope.row.icon" alt="" style="width:40px;height:40px">
            </template>
          </el-table-column>
          <el-table-column align="center" prop="game_ids" label="游戏" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-for="(item,index) in scope.row.game_ids" :key="index">
                {{ gameLabel(item) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column align="left" fixed="right" prop="operation" label="操作" width="150">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="'game.edit'" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)" icon="el-icon-edit-outline" />
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'game.delete'" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click="deleteItem(scope.row)" icon="el-icon-delete"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination  :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
      <page-details :show="isShowDetails" @onHideDetails="onHideDetails" :data="checkedItem" :languageArray="languageDataArray"/>
    </div>
  </div>
</template>

<script>
import { throttle } from '@/utils/index'
import {
  getNavigation,
  delNavigation
} from '@/api/game'
import {
  GAME_TYPE
} from '@/utils/enum'
import PageDetails from './page-details.vue'
import SearchTable from '@/components/SearchTable'

export default {
  name: 'navigationManagement',
  components: {
    PageDetails,
    SearchTable
  },
  data () {
    return {
      GAME_TYPE,
      dataList:[],
      languageDataArray: this.$store.state.commonState.languageDataArray,
      languageArrayMap: new Map(),
      multilingualData: [],
      cornerData:[
        {label:'不显示',value:0},
        {label:'新游戏',value:1},
        {label:'火爆',value:2},
      ],
      positionData:[
        {label:'普通游戏位',value:1},
        {label:'推广游戏位',value:2},
      ],
      imageData:[],
      checkedItem: null,
      listLoading: false,
      isShowDetails: false,
      isDisabledGame: false,
      isShowTypeDetails: false,
      gameType:1,
      terminalType:1,
      currentTotal: 0,
      listQuery: {
        page: 1,
        size: 50,
        game_id:0,
      },
    }
  },
  created () {
    const languageArrayMap = new Map();
    this.languageDataArray.forEach(s => {
      languageArrayMap.set(s.value, s.label);
    });
    this.languageArrayMap = languageArrayMap;
  },
  activated () {
    this.languageDataArray=this.$store.state.commonState.languageDataArray
    this.getDataList()
  },
  methods: {
    // 获取游戏设置列表
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = [];
      that.checkedItem = null;
      that.listLoading = true;
      const response = await getNavigation(params)
      if (response.code==1) {
        if (response.data.list) {
          response.data.list.forEach(item => {
            item.isChecked = false
            let arr = item.game_ids.split(',')
            item.game_ids = []
            arr.map(second=>{
              item.game_ids.push(Number(second))
            })
          })
          that.currentTotal = response.data.total
          that.dataList = response.data.list
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false
    },
    // 编辑
    editItem (row) {
      let that = this;
      this.isShowDetails = true
      that.checkedItem = row
      that.handleCurrentChange(row)
    },
    // 删除
    deleteItem (row) {
      const that = this
      that.handleCurrentChange(row)
      that.$confirm(`是否删除【${that.languageArrayMap.get(scope.row.name)}】配置?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delNavigation({
          id: row.id
        }).then(res => {
          if (res.code == 1) {
            that.onHideDetails(true)
            that.$message({
              type: 'success',
              message: res.msg
            })
          }
        })
      }).catch((err) => {})
    },
    // 选择配置类型
    handleConfirm(){
      this.isShowDetails = true
      this.isShowTypeDetails = false
    },
    // 图片配置
    appLabel(cellValue){
      return (this.positionData.find((item) => item.value == cellValue)?.label || "-");
    },
    // 游戏类型
    gameLabel(cellValue){
      return (this.GAME_TYPE.find((item) => item.value == cellValue)?.label || "-");
    },
    // 选择表格数据
    handleCurrentChange (val) {
      this.checkedItem = val;
      this.dataList.map(item => {
        if (item.id === val.id) {
          item.isChecked = true
        } else {
          item.isChecked = false
        }
      })
    },
    // 隐藏详情
    onHideDetails (show) {
      this.isShowDetails = false
      this.isShowTypeDetails = false
      if (show) {
        this.getDataList(true)
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
        game_id:0,
      }
      this.getDataList();
    }, 2000),
  }
}
</script>

<style lang="scss" scoped>
</style>
